﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Twittit.Business
{
    class TwittitTagService
    {
        private Twittit.DataAccess.DatabaseEntities data;
        private static TwittitTagService instance;

        private TwittitTagService()
        {
            data = new Twittit.DataAccess.DatabaseEntities();
        }

        public static TwittitTagService getInstance()
        {
            if (instance == null)
                instance = new TwittitTagService();
            return instance;
        }

        // Retrieve TwittitTag create if not found
        public Twittit.DataAccess.TwittitTag getTwittitTag(int twittit_id, Twittit.DataAccess.Tag tag)
        {
            Twittit.DataAccess.TwittitTag tt;
            try
            {
                tt = data.TwittitTag.Where(t => t.tag_id == tag.id).Where(t => t.twittit_id == twittit_id).First();
            }
            catch(Exception)
            {
                Twittit.DataAccess.TwittitTag t = new Twittit.DataAccess.TwittitTag();
                t.twittit_id = twittit_id;
                t.tag_id = tag.id;
                data.TwittitTag.AddObject(t);

                tt = t;
            }
            data.SaveChanges();
            return tt;
        }

        public void remove(Twittit.DataAccess.TwittitTag t)
        {
            data.TwittitTag.DeleteObject(t);
            data.SaveChanges();
        }
    }

}
